package com.hyc.algorithm.BigSum;

/**
 * @projectName: DataStructure
 * @package: com.hyc.algorithm.BigSum
 * @className: bigSum
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 * @date: 2022/4/4 16:00
 * @version: 1.0
 */
public class bigSum {
    public static void main(String[] args) {
        String a = "2222222";
        String b = "999999";
        System.out.println(add(a, b));
    }

    public static String add(String a, String b) {
        //数a的倒数第i位置
        int ai = 1;
        //数b的倒数第i位置
        int bi = 1;
        //进位标志
        int flag = 0;
        //返回结果
        String result = "";
        while (ai <= a.length() || bi <= b.length() || flag > 0) {
            //-‘0’ 是为了得到这个数 例子: 字符 1 = 49 字符 0 = 48 相减得 1
            int m = ai <= a.length() ? a.charAt(a.length() - ai) - '0' : 0;
            int n = bi <= b.length() ? b.charAt(b.length() - bi) - '0' : 0;
            System.out.println(m + "+" + n + "+" + flag + "=" + (m + n + flag) % 10);
            result = (m + n + flag) % 10 + result;
            flag = (m + n + flag) / 10;
            ai++;
            bi++;
        }
        return result;
    }
}
